#include <stdio.h>
#include <ctype.h>
#include "ofc/DTokenizer.h"
#include "ofc/DFile.h"

@interface MyTokenizer : DTokenizer           // MyTokenizer overrides comment and extraToken1
{
@private
}


- (DText *) comment :(const char *) text;     // Override the comment checker
- (DText *) extraToken1 :(const char *) text; // Override the extra token checker
@end

@implementation MyTokenizer 

- (DText *) comment :(const char *) text      // Override the default comment checker ..
{
  DText *scanned = nil;
  
  if ((*text == '/') && (*(text+1) == '/'))   // .. comment is '//' 
  {
    scanned = [DText new];
    
    while (*text != EOS)
    {
      [scanned push :*text++];
    }
  }
  return scanned;
}

- (DText *) extraToken1 :(const char *) text  // Override the extra token checker for ..
{
  DText *scanned = nil;

  if (*text == '#')                           // .. scanning of directives: #...
  {
    scanned = [DText new];

    while ((!isspace(*text)) && (*text != EOS))
    {
      [scanned push :*text++];
    }
  }
  return scanned;
}

@end


int main(int argc, char *argv[])
{
  MyTokenizer *srce = [MyTokenizer new];
  DFile       *file = [DFile       new];

  if ([file open :"example.tok" :"r"]) // Open the file
  { 
    if ([srce source :file :"example.tok"]) // Give the file to the tokenizer
    {
      [srce skipWhiteSpace :YES];      // Skip whitespace

      while (![srce isEof])            // Do for the whole
      {
        int token = [srce nextToken];  // Get the next token and ..

        switch (token)                 // .. print the tokenized info
        {
          case DTK_EXTRA1    : printf("Directive: %s\n", [srce text]); break;
          case DTK_COMMENT   : printf("Comment  : %s\n", [srce text]); break;
          case DTK_NUMBER    : printf("Number   : %s\n", [srce text]); break;
          case DTK_KEYWORD   : printf("Keyword  : %s\n", [srce text]); break;
          case DTK_STRING    : printf("String   : %s\n", [srce text]); break;
          case DTK_OPERATOR  : printf("Operator : %s\n", [srce text]); break;
          default:             printf("Unkown   : %s\n", [srce text]); break;
        }
      }
    }
    else
      printf("File \"example.tok\" could not be used by tokenizer.\n");

    [file close];
  }
  else
    printf("File \"example.tok\" could not be opened:%d.\n", [file error]);

  [file free];                         // Cleanup
  [srce free];

  return 0;
}

